<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="listQuery.title" placeholder="赛事名称" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-button class="filter-item" type="primary" icon="el-icon-search">
        搜索
      </el-button>
      <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-edit" @click="handleCreate">
        项目立项
      </el-button>
    </div>

    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      style="width: 100%; margin-top:10px;"
      highlight-current-row
    >
      <el-table-column align="center" label="序号" width="100" prop="id">
        <template slot-scope="scope">
          {{ scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column label="赛事名称" width="150" align="center">
        <template slot-scope="scope">
          {{ scope.row.matchName }}
        </template>
      </el-table-column>
      <el-table-column label="组赛单位" width="100" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.matchUnit }}</span>
        </template>
      </el-table-column>
      <el-table-column label="赛制" width="100" align="center">
        <template slot-scope="scope">
          <div v-if="scope.row.matchRules === 0">
            个人赛
          </div>
          <div v-else>
            团队赛
          </div>
        </template>
      </el-table-column>
      <el-table-column label="项目负责人" width="150" align="center">
        <template slot-scope="scope">
          {{ scope.row.subLeader }}
        </template>
      </el-table-column>
      <el-table-column label="联系电话" width="150" align="center">
        <template slot-scope="scope">
          {{ scope.row.telephone }}
        </template>
      </el-table-column>
      <el-table-column label="电子邮箱" width="150" align="center">
        <template slot-scope="scope">
          {{ scope.row.email }}
        </template>
      </el-table-column>
      <el-table-column label="状态" width="150" align="center">
        <template slot-scope="scope">
          <a v-if="scope.row.subStatus === 0">未审核</a>
          <a v-if="scope.row.subStatus === 1 || scope.row.subStatus === 3">通过</a>
          <a v-if="scope.row.subStatus === 2">不通过</a>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="250px" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="handleView(scope.row.subId)">
            详情
          </el-button>
          <el-button v-if="scope.row.matchRules === 0 && scope.row.subStatus === 1" type="primary" size="mini" @click="handleMember(scope.row.subId)">
            添加成员
          </el-button>
          <el-button v-if="scope.row.matchRules === 1 && scope.row.subStatus === 1" type="primary" size="mini" @click="handleTeam(scope.row.subId)">
            添加团队
          </el-button>
          <el-button v-if="scope.row.subStatus != 1 && scope.row.subStatus != 3" size="mini" type="danger" @click="handleDelete(scope.row)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.pageNum" :limit.sync="listQuery.pageSize" @pagination="fetchData" />

    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form ref="dataForm" :model="temp" label-position="left" label-width="70px" style="width: 500px; margin-left: 50px;">
        <el-form-item label="赛事名称" prop="matchName" label-width="120px">
          <el-input v-model="temp.matchName" />
        </el-form-item>
        <el-form-item label="组赛单位" prop="matchUnit" label-width="120px">
          <el-input v-model="temp.matchUnit" />
        </el-form-item>
        <el-form-item label="赛制" prop="matchRules" label-width="120px">
          <el-radio v-model="temp.matchRules" label="0">个人赛</el-radio>
          <el-radio v-model="temp.matchRules" label="1">团队赛</el-radio>
        </el-form-item>
        <el-form-item label="联系电话" prop="telephone" label-width="120px">
          <el-input v-model="temp.telephone" />
        </el-form-item>
        <el-form-item label="电子邮箱" prop="email" label-width="120px">
          <el-input v-model="temp.email" />
        </el-form-item>
        <el-form-item label="竞赛开始时间" prop="matchStartTime" label-width="120px">
          <el-col :span="11">
            <el-date-picker v-model="temp.matchStartTime" type="datetime" placeholder="选择日期" value-format="yyyy-MM-dd HH:mm:ss" />
          </el-col>
        </el-form-item>
        <el-form-item label="竞赛结束时间" prop="matchEndTime" label-width="120px">
          <el-col :span="11">
            <el-date-picker v-model="temp.matchEndTime" type="datetime" placeholder="选择日期" value-format="yyyy-MM-dd HH:mm:ss" />
          </el-col>
        </el-form-item>
        <el-form-item label="专业" prop="major" label-width="120px">
          <el-input v-model="temp.major" />
        </el-form-item>
        <el-form-item label="竞赛主办单位" prop="matchHostUnit" label-width="120px">
          <el-input v-model="temp.matchHostUnit" />
        </el-form-item>
        <el-form-item label="竞赛承办单位" prop="matchUndertakeUnit" label-width="120px">
          <el-input v-model="temp.matchUndertakeUnit" />
        </el-form-item>
        <el-form-item label="论证目的和意义" prop="meaning" label-width="120px">
          <el-input v-model="temp.meaning" type="textarea" />
        </el-form-item>
        <el-form-item label="资金预算" label-width="120px" :model="free">
          <div class="" style="width:100%;display:flex;justify-content: space-between;">
            <div style="width:20%">
              <table width="100%">
                <table width="100%">
                  <tr><td class="left_title bottom_border">注册费</td></tr>
                  <tr><td class="left_title bottom_border">差旅费</td></tr>
                  <tr><td class="left_title bottom_border">培训费</td></tr>
                  <tr><td class="left_title bottom_border">评审费</td></tr>
                  <tr><td class="left_title bottom_border">领队费</td></tr>
                  <tr><td class="left_title bottom_border">组织费</td></tr>
                  <tr><td class="left_title bottom_border">指导费</td></tr>
                  <tr><td class="left_title bottom_border">材料费</td></tr>
                  <tr><td class="left_title bottom_border">教师奖金</td></tr>
                  <tr><td class="left_title bottom_border">合计费用</td></tr>
                </table>
              </table>
            </div>
            <div>
              <table width="100%">
                <tr><td class="left_title bottom_border"><input v-model="free.registerFree" style="width:300px"></td></tr>
                <tr><td class="left_title bottom_border"><input v-model="free.businessFree" style="width:300px"></td></tr>
                <tr><td class="left_title bottom_border"><input v-model="free.trainFree" style="width:300px"></td></tr>
                <tr><td class="left_title bottom_border"><input v-model="free.reviewFree" style="width:300px"></td></tr>
                <tr><td class="left_title bottom_border"><input v-model="free.leaderFree" style="width:300px"></td></tr>
                <tr><td class="left_title bottom_border"><input v-model="free.organizeFree" style="width:300px"></td></tr>
                <tr><td class="left_title bottom_border"><input v-model="free.guideFree" style="width:300px"></td></tr>
                <tr><td class="left_title bottom_border"><input v-model="free.materialFree" style="width:300px"></td></tr>
                <tr><td class="left_title bottom_border"><input v-model="free.teacherBonus" style="width:300px"></td></tr>
                <tr><td class="left_title bottom_border"><input v-model="free.sumFree" style="width:300px"></td></tr>
              </table>
            </div>
          </div>
        </el-form-item>
        <el-form-item label="通知附件" prop="file" label-width="120px">
          <el-upload
            ref="upload"
            class="upload-demo"
            :data="temp"
            :multiple="false"
            :headers="myHeaders"
            :on-success="handleFileSuccess"
            action="http://localhost:8088/subject/leader/add"
            :auto-upload="false"
          >
            <i class="el-icon-upload" />
            <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="createData()">
          确定
        </el-button>
      </div>
    </el-dialog>

    <el-dialog title="上传立项申请书" :visible.sync="uploadVisible">
      <el-item label="立项申请书" label-width="120px">
        <el-upload
          ref="upload1"
          class="upload-demo"
          :data="uploaddata"
          :multiple="false"
          :headers="myHeaders"
          :on-success="uploadFileSuccess"
          action="http://localhost:8088/subject/leader/upload"
          :auto-upload="false"
        >
          <i class="el-icon-upload" />
          <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        </el-upload>
      </el-item>
      <div slot="footer" class="dialog-footer">
        <el-button @click="uploadVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="upload()">
          确定
        </el-button>
      </div>
    </el-dialog>

    <el-dialog title="详情" :visible.sync="viewVisible">
      <div>
        意见：
        <a>
          {{ temp.opinion }}
        </a>
      </div>
      <div style="margin-top:40px">
        <el-button type="primary" size="mini" @click="handleDownLoad(subId)">
          下载申请书
        </el-button>
        <el-button type="primary" size="mini" @click="handleUpload(subId)">
          上传申请书
        </el-button>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="viewVisible = false">
          取消
        </el-button>
      </div>
    </el-dialog>
    <!-- 添加成员的页面  -->
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="memberVisible">
      <el-form ref="dataForm" :model="member" label-position="left" label-width="70px" style="width: 400px; margin-left: 50px;">
        <!-- 隐藏id -->
        <el-form-item label="学号" prop="stuId" label-width="100px">
          <el-input v-model="member.stuId" />
        </el-form-item>
        <el-form-item label="姓名" prop="stuName" label-width="100px">
          <el-input v-model="member.stuName" />
        </el-form-item>
        <el-form-item label="学院" prop="instName" label-width="100px">
          <el-input v-model="member.instName" />
        </el-form-item>
        <el-form-item label="班级" prop="className" label-width="100px">
          <el-input v-model="member.className" />
        </el-form-item>
        <el-form-item label="年级" prop="grade" label-width="100px">
          <el-input v-model="member.grade" />
        </el-form-item>
        <el-form-item label="专业" prop="major" label-width="100px">
          <el-input v-model="member.major" />
        </el-form-item>
        <el-form-item label="邮箱" prop="email" label-width="100px">
          <el-input v-model="member.email" />
        </el-form-item>
        <el-form-item label="手机号" prop="telephone" label-width="100px">
          <el-input v-model="member.telephone" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="memberVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="createMember()">
          确定
        </el-button>
      </div>
    </el-dialog>
    <!-- 添加团队的页面  -->
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="teamVisible">
      <el-form ref="dataForm" :model="team" label-position="left" label-width="70px" style="width: 400px; margin-left: 50px;">
        <!-- 隐藏id -->
        <el-form-item label="团队名称" prop="userId" label-width="100px">
          <el-input v-model="team.teamName" />
        </el-form-item>
      </el-form>
      <el-button @click="teamMember()">
        添加队员
      </el-button>
      <el-table :data="team.memberList">
        <el-table-column label="学号" align="center" prop="stuId" />
        <el-table-column label="姓名" align="center" prop="stuName" :show-overflow-tooltip="true" />
        <el-table-column label="学院" align="center" prop="instName" :show-overflow-tooltip="true" />
        <el-table-column label="班级" align="center" prop="className" :show-overflow-tooltip="true" />
        <el-table-column label="年级" align="center" prop="grade" />
        <el-table-column label="专业" align="center" prop="major" :show-overflow-tooltip="true" />
        <el-table-column label="邮箱" align="center" prop="email" :show-overflow-tooltip="true" />
        <el-table-column label="手机号" align="center" prop="telephone" />
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="createTeam()">
          确定
        </el-button>
      </div>
    </el-dialog>
    <!-- 添加团队成员的页面  -->
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="teamMemberVisible">
      <el-form ref="dataForm" :model="member" label-position="left" label-width="70px" style="width: 400px; margin-left: 50px;">
        <!-- 隐藏id -->
        <el-form-item label="学号" prop="stuId" label-width="100px">
          <el-input v-model="member.stuId" />
        </el-form-item>
        <el-form-item label="姓名" prop="stuName" label-width="100px">
          <el-input v-model="member.stuName" />
        </el-form-item>
        <el-form-item label="学院" prop="instName" label-width="100px">
          <el-input v-model="member.instName" />
        </el-form-item>
        <el-form-item label="班级" prop="className" label-width="100px">
          <el-input v-model="member.className" />
        </el-form-item>
        <el-form-item label="年级" prop="grade" label-width="100px">
          <el-input v-model="member.grade" />
        </el-form-item>
        <el-form-item label="专业" prop="major" label-width="100px">
          <el-input v-model="member.major" />
        </el-form-item>
        <el-form-item label="邮箱" prop="email" label-width="100px">
          <el-input v-model="member.email" />
        </el-form-item>
        <el-form-item label="手机号" prop="telephone" label-width="100px">
          <el-input v-model="member.telephone" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="teamMemberVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="pushMember()">
          确定
        </el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { LeaderGetList, download, deleteSub, getOne } from '@/api/subject'
import { addMember } from '@/api/member'
import { addTeam } from '@/api/team'
import Pagination from '@/components/Pagination'
import { getToken } from '@/utils/auth'
import Vue from 'vue'

export default {
  components: { Pagination },
  data() {
    return {
      myHeaders: { Authorization: getToken() },
      listQuery: {
        pageNum: 1,
        pageSize: 20,
        title: undefined,
        leaderId: this.$store.getters.name
      },
      list: null,
      total: 0,
      listLoading: true,
      textMap: {
        update: '修改',
        create: '项目立项'
      },
      dialogStatus: '',
      dialogFormVisible: false,
      uploadVisible: false,
      viewVisible: false,
      memberVisible: false,
      teamVisible: false,
      teamMemberVisible: false,
      temp: {
        id: '',
        subId: '',
        matchName: '',
        matchUnit: '',
        matchRules: '',
        subLeader: this.$store.getters.name,
        telephone: '',
        email: '',
        matchStartTime: '',
        matchEndTime: '',
        major: '',
        matchHostUnit: '',
        matchUndertakeUnit: '',
        meaning: ''
      },
      free: {
        registerFree: '',
        businessFree: '',
        trainFree: '',
        reviewFree: '',
        leaderFree: '',
        organizeFree: '',
        guideFree: '',
        materialFree: '',
        teacherBonus: '',
        sumFree: ''
      },
      uploaddata: {
        subId: ''
      },
      subId: '',
      member: {},
      team: {
        memberList: []
      }
    }
  },
  created() {
    this.fetchData()
    this.listLoading = false
  },
  methods: {
    apiUrl() {
      return 'http://localhost:8088/subject/leader/add'
    },
    handleFilter() {
      this.listQuery.pageNum = 1
      if (this.listQuery.title === null) {
        this.listQuery.title = undefined
      }
      this.fetchData()
    },
    fetchData() {
      this.listLoading = true
      LeaderGetList(this.listQuery).then(response => {
        this.list = response.data.content
        this.total = response.data.totalSize
        this.listLoading = false
      })
    },
    resetTemp() {
      this.temp = {
        matchName: '',
        matchUnit: '',
        matchRules: '',
        subLeader: this.$store.getters.name,
        telephone: '',
        email: '',
        matchStartTime: '',
        matchEndTime: '',
        major: '',
        matchHostUnit: '',
        matchUndertakeUnit: '',
        meaning: ''
      }
      this.free = {
        registerFree: '',
        businessFree: '',
        trainFree: '',
        reviewFree: '',
        leaderFree: '',
        organizeFree: '',
        guideFree: '',
        materialFree: '',
        teacherBonus: '',
        sumFree: ''
      }
    },
    handleCreate() {
      this.resetTemp()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    createData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          Vue.set(this.temp, 'free', JSON.stringify(this.free))
          this.$refs.upload.submit()
        }
      })
    },
    handleFileSuccess(response, file) {
      if (response.code === 200) {
        setTimeout(() => {
          this.list.push(response.data)
          this.dialogFormVisible = false
          this.$refs.upload.clearFiles()
          this.handleFilter()
          this.$notify({
            title: 'Success',
            message: '添加成功',
            type: 'success',
            duration: 2000
          })
        })
      } else {
        this.$refs.upload.clearFiles()
        this.$notify.error({
          message: '添加失败',
          type: 'error'
        })
      }
    },
    handleDownLoad(subId) {
      download(subId).then(response => {
        const fileName = '立项申请书' + '.' + 'doc'
        const data = response
        const blob = new Blob([data], { type: 'multipart/form-data' })
        const url = window.URL.createObjectURL(blob)
        const link = document.createElement('a')
        link.style.display = 'none'
        link.href = url
        link.setAttribute('download', fileName)
        document.body.appendChild(link)
        link.click()
        this.$notify({
          title: 'Success',
          message: '下载成功',
          type: 'success',
          duration: 2000
        })
      })
    },
    handleUpload(subId) {
      this.uploadVisible = true
      this.uploaddata.subId = subId
    },
    upload() {
      this.$refs.upload1.submit()
    },
    uploadFileSuccess(response) {
      if (response.code === 200) {
        setTimeout(() => {
          this.uploadVisible = false
          this.$refs.upload1.clearFiles()
          this.$notify({
            title: 'Success',
            message: '添加成功',
            type: 'success',
            duration: 2000
          })
        })
      } else {
        this.$refs.upload1.clearFiles()
        this.$notify.error({
          message: '添加失败',
          type: 'error'
        })
      }
    },
    handleDelete(row) {
      this.dialogdeleteVisible = true
      this.$confirm('此操作将永久删除该项目, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteSub(row.subId).then(() => {
          this.fetchData
          this.$notify({
            title: 'Success',
            message: '删除成功',
            type: 'success',
            duration: 2000
          })
        })
      })
    },
    handleView(subId) {
      getOne(subId).then(response => {
        this.temp = response.data
      })
      this.subId = subId
      this.viewVisible = true
    },
    handleMember(subId) {
      this.member = {}
      this.member.subId = subId
      this.memberVisible = true
    },
    createMember() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          const tempData = Object.assign({}, this.member)
          addMember(tempData).then(() => {
            this.memberVisible = false
            this.$notify({
              title: 'Success',
              message: '添加成功',
              type: 'success',
              duration: 2000
            })
          })
        }
      })
    },
    handleTeam(subId) {
      this.team.subId = subId
      this.teamVisible = true
    },
    teamMember() {
      this.member = {}
      this.teamMemberVisible = true
    },
    pushMember() {
      this.team.memberList.push(this.member)
      this.teamMemberVisible = false
    },
    createTeam() {
      addTeam(this.team).then(() => {
        this.teamVisible = false
        this.$notify({
          title: 'Success',
          message: '添加成功',
          type: 'success',
          duration: 2000
        })
      })
    }
  }
}
</script>
